//
//  YRNKCycleStyleProgressView.swift
//  YoruNeko
//
//  Created by liphone on 2021/6/11.
//

import Foundation
import UIKit
import SwiftUI

final class YRNKCycleStyleProgressView:UIView, UIViewRepresentable {
    
    
    var radius:CGFloat //定义圆形半径
    var boldValue:CGFloat //定义粗细
    var color: UIColor //定义颜色
    var progress:CGFloat //0~1.0
    
    private var shapeLayer:CAShapeLayer?
    

    
    
    func makeUIView(context: Context) -> UIView {
        mydraw(self.frame)
        return self
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        //uiView.setNeedsDisplay()
        
        //这里会生成两份YRNKCycleStyleProgressView, swiftUI 获取了对该对象的copy
        if let view = uiView as? YRNKCycleStyleProgressView {
            view.shapeLayer?.strokeEnd = min(1.0,progress)
        }
    }
  
    init(radius: CGFloat,boldValue:CGFloat,color:UIColor,progress:Float){
        self.boldValue=boldValue;
        self.radius=radius;
        self.color=color
        self.progress = CGFloat.init(progress)
        super.init(frame: CGRect(origin: .zero, size: CGSize(width: 2*radius*1.2, height: 2*radius*1.2)))
    }
    
    required init?(coder: NSCoder) {
        self.boldValue=10
        self.radius=20
        self.color = .blue
        self.progress = 0
        super.init(coder: coder)
    }
    
    func mydraw(_ rect: CGRect) {

        shapeLayer = CAShapeLayer.init()
        if let shape_layer=shapeLayer{
            self.layer.addSublayer(shape_layer)
            
            
            
            
            shape_layer.strokeColor = color.cgColor
            shape_layer.lineWidth = self.boldValue
            shape_layer.fillColor = .none
            shape_layer.frame = frame
            
            
            
            let path = UIBezierPath() //贝塞尔曲线
            let center = CGPoint(x: frame.width*0.5,y:frame.height*0.5)
            let PI =  Double.pi
            //let end_angle = CGFloat(PI*2)*progress - CGFloat(0.5*PI)
            //画圆弧
            path.addArc(withCenter: center, radius: self.radius, startAngle: CGFloat(-PI*0.5), endAngle:0.75*2*CGFloat(PI), clockwise: true)
            path.lineCapStyle = .round
            path.lineJoinStyle = .round
            
            shape_layer.path = path.cgPath
            shape_layer.strokeStart = 0
            shape_layer.strokeEnd = progress
            
            //try animation here
            //let animation:CAKeyframeAnimation = CAKeyframeAnimation(keyPath:"strokeEnd")
    //        animation.path = path.cgPath;
    //        animation.duration = 0.25
    //        animation.isRemovedOnCompletion = true
    //        animation.fillMode = .forwards
    //        shapeLayer.add(animation, forKey: nil)
        }
        
        
    }
    
    func setProgress(progress:CGFloat){
        self.progress = min(1.0, progress)
        self.shapeLayer?.strokeEnd = progress
        //self.setNeedsDisplay()
    }
    
//    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//        //holder
//        if(keyPath == "downloadedSize" && object.customMirror.subjectType == DownloadViewItem.Type.self){
//            let item = object as! DownloadViewItem
//            let progress = item.progress
//            NSLog("[YRNKCycleViewDetectValueChanged on download item view, progress = %f",progress)
//            guard let shape_layer = self.shapeLayer else {
//                NSLog("[YRNKCycleStyleProgressView: ] has no shape layer ")
//                return
//            }
//
//            shape_layer.strokeEnd = CGFloat(max(1.0,progress))
//        }
//    }
    
}
